package com.example.demo.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface CompatibilityMapper {
    // Scenario 0: cleanup helper
    int dropDateDefaultTable();

    int dropExplicitTimestampTable();

    int dropUpsertSource();

    int dropUpsertTarget();

    // Scenario 1: DATE default CURRENT_TIMESTAMP (expected to fail on GoldenDB per spec and typically unsupported on MySQL)
    int createDateDefaultTable();

    // Scenario 2: explicit_defaults_for_timestamp - create table with NOT NULL + DEFAULT CURRENT_TIMESTAMP
    int createExplicitTimestampTable();

    // Read server variable to see mode
    @Select("SELECT @@explicit_defaults_for_timestamp")
    int getExplicitDefaultsFlag();

    // Insert NULL into timestamp column to observe behavior difference
    @Update("INSERT INTO t_explicit_ts (id, ts) VALUES (#{id}, NULL)")
    int insertNullIntoTimestamp(@Param("id") long id);

    // Scenario 3a: WITH CTE query (supported in MySQL 8.0+, risky on GoldenDB per spec)
    @Select("WITH u AS (SELECT id FROM `user`) SELECT COUNT(*) FROM u")
    int withCteCount();

    // Scenario 3b: MERGE INTO example (avoid in GoldenDB; not supported by MySQL). Provided for GoldenDB-only verification
    int createUpsertTarget();

    int createUpsertSource();

    int seedUpsertSource();

    int seedUpsertTarget();

    @Update("MERGE INTO t_upsert_target t USING t_upsert_source s ON (t.id = s.id) WHEN MATCHED THEN UPDATE SET t.val = s.val WHEN NOT MATCHED THEN INSERT (id, val) VALUES (s.id, s.val)")
    int mergeIntoExample();
}


